<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>题目管理</el-breadcrumb-item>
      <el-breadcrumb-item>题目列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col>
          <!-- 警告提示 -->
          <el-alert title="编辑题目需要重新审核，请注意！" type="warning" center show-icon id="warning"></el-alert>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="articleList" stripe border>
        <el-table-column type="index" label="#"></el-table-column>
        <!-- 展开行 -->
        <el-table-column type="expand" label="展开内容" width="80">
          <!-- 展开行的作用域插槽 -->
          <template slot-scope="scope">
            <el-row>
              <el-col :span="2">
                <el-tag type="success">设计内容：</el-tag>
              </el-col>
              <el-col :span="12">
                <el-input
                  type="textarea"
                  :rows="10"
                  placeholder="请输入内容"
                  v-model="scope.row.content"
                  readonly
                ></el-input>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column prop="_id" label="题目id"></el-table-column>
        <el-table-column prop="title" label="题目名称"></el-table-column>
        <el-table-column prop="Tauthor.username" label="指导老师"></el-table-column>
        <el-table-column prop="Tauthor.email" label="指导老师邮箱"></el-table-column>
        <el-table-column prop="publishDate" label="发布时间">
          <!-- 发布时间作用域插槽 -->
          <template slot-scope="scope">{{scope.row.publishDate | dateFormat}}</template>
        </el-table-column>
        <el-table-column prop="checked" label="审核状态" width="80">
          <!-- 审核作用域插槽 -->
          <template slot-scope="scope">
            <el-tag v-if="scope.row.checked === true" type="success">通过</el-tag>
            <el-tag v-else type="danger">未通过</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <!-- 作用域插槽 -->
          <template slot-scope="scope">
            <!-- 编辑按钮 -->
            <el-button
              size="mini"
              icon="el-icon-edit"
              type="primary"
              @click="showEditDialog(scope.row)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              size="mini"
              icon="el-icon-delete"
              type="danger"
              @click="delteArticleById(scope.row._id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 编辑对话框 -->
      <el-dialog title="编辑题目" :visible.sync="editDialogVisible" width="50%" close="resetEditForm">
        <!-- 警告提示 -->
        <el-alert title="编辑题目需要重新审核，请注意！" type="warning" center show-icon></el-alert>
        <!-- 编辑表单 -->
        <el-form
          :model="editForm"
          :rules="editRules"
          ref="editFormRef"
          label-width="100px"
          label-position="top"
        >
          <el-form-item label="题目标题" prop="title">
            <el-input v-model="editForm.title"></el-input>
          </el-form-item>
          <el-form-item label="题目内容" prop="content">
            <el-input type="textarea" :rows="15" placeholder="请输入内容" v-model="editForm.content"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editArticle">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 文章列表
      articleList: [],
      // 登陆用户的id
      id: '',
      // 请求参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      // 控制编辑对话框的显示与否
      editDialogVisible: false,
      // 编辑表单对象
      editForm: {
        title: '',
        content: ''
      },
      // 编辑表单验证规则
      editRules: {
        title: [{ required: true, message: '请输入题目标题', trigger: 'blur' }],
        content: [
          { required: true, message: '请输入题目内容', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 获取文章列表
    this.getArticleList()
  },
  methods: {
    async getArticleList() {
      // 获取登陆用户的id
      this.id = this.$store.state.uid
      console.log(this.id)
      // 发送请求
      const { data: res } = await this.$http.get(
        'articles/teacher/' + this.id,
        { params: this.queryInfo }
      )
      // 判断请求结果
      if (res.meta.status !== 200) {
        return this.$message.error('获取文章列表失败')
      }
      // 请求成功，赋值
      this.articleList = res.data.records
      console.log(this.articleList)
      this.total = res.data.total
    },
    // 每页显示条数改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      // 重新获取题目列表
      this.getArticleList()
    },
    // 当前页码改变
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      // 重新获取题目列表
      this.getArticleList()
    },
    // 显示编辑对话框
    async showEditDialog(row) {
      // 根据id参数题目
      const { data: res } = await this.$http.get('articles/' + row._id)
      // 判断请求结果
      if (res.meta.status !== 200) {
        return this.$message.error('获取数据失败')
      }
      // 请求成功，给editForm表单对象赋值
      this.editForm = res.data
      this.editDialogVisible = true
    },
    // 编辑标题提交
    editArticle() {
      // 表单预验证
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) {
          return this.message.error('表单验证失败')
        }
        // 验证通过
        console.log(this.editForm)
        const { data: res } = await this.$http.put(
          'articles/edit/' + this.editForm._id,
          {
            title: this.editForm.title,
            content: this.editForm.content,
            role: 'teacher'
          }
        )
        // 判断请求结果
        if (res.meta.status !== 200) {
          return this.$message.error('修改失败')
        }
        // 提交成功，给出提示，重新加载数据
        this.$message.success('修改成功')
        this.getArticleList()
        this.editDialogVisible = false
      })
    },
    // 根据id删除题目
    async delteArticleById(id) {
      // 确认框
      const confirmResult = await this.$confirm(
        '此操作将永久删除该题目, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      // 如果点击了确认,发送请求
      if (confirmResult === 'confirm') {
        const { data: res } = await this.$http.delete('articles/delete/' + id)
        // 判断请求结果
        if (res.meta.status !== 200) {
          this.$message.error('删除失败')
        }
        // 删除成功,给出提示,重新加载题目列表
        this.$message.success('删除成功')
        this.getArticleList()
      } else {
        // 如果点击了取消
        this.$message.info('已取消了删除操作')
      }
    },
    // 对话框关闭事件
    resetEditForm() {
      // 重置表单
      this.$refs.editFormRef.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
#warning {
  margin-bottom: 15px;
}
</style>
